<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="部门" prop="orgId">
        <el-select v-model="dataForm.orgId" placeholder="部门">
          <el-option v-for="item in dataForm.orgList" :key="item.orgId" :label="item.orgName" :value="item.orgId">
            <template sloe-scope="{item}" >
                {{item.orgName}}
            </template>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="人员级别" prop="empLevel">
        <el-input v-model="dataForm.empLevel" placeholder="人员级别"></el-input>
      </el-form-item>
      <el-form-item label="人员类型" prop="empType">
        <el-input v-model="dataForm.empType" placeholder="人员类型"></el-input>
      </el-form-item>
      <el-form-item label="人员姓名" prop="empName">
        <el-input v-model="dataForm.empName" placeholder="人员姓名"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          empName: '',
          empLevel: 1,
          empType: '',
          orgId: 1,
          orgList:[]
        },
        dataRule: {
          empName: [
            { required: true, message: '人员姓名不能为空', trigger: 'blur' }
          ],
          orgId: [
            { required: true, message: '所属部门不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/sys/employee/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.empLevel = data.employee.empLevel;
              this.dataForm.empType = data.employee.empType;
              this.dataForm.empName = data.employee.empName;
              this.dataForm.orgId = data.employee.orgId;
              this.dataForm.orgList = data.orgList;
              this.visible = true;
            }
          })
        }else{
            this.visible = true;
            this.$nextTick(() => {
              this.$refs['dataForm'].resetFields();
              this.$http({
                url: this.$http.adornUrl(`/sys/employee/info/0`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm.orgList = data.orgList;
                }
              })
            })
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/employee/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'empLevel': this.dataForm.empLevel,
                'empType': this.dataForm.empType,
                'empName': this.dataForm.empName,
                'orgId': this.dataForm.orgId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
